package com.onetime.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author Lenovo
 */
@Configuration
public class MallCorsConfiguration {
    /**
     * 加入到容器中
     *
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        /*配置跨域*/
        /*允许访问的头信息,*表示全部*/
        corsConfiguration.addAllowedHeader("*");
        /*允许提交请求的方法，*表示全部允许，也可以单独设置GET、PUT等*/
        corsConfiguration.addAllowedMethod("*");
        /*允许向该服务器提交请求的URI，*表示全部允许。。这里尽量限制来源域，比如http://xxxx:8080 ,以降低安全风险。*/
        corsConfiguration.addAllowedOriginPattern("*");
        /*是否允许携带cookie进行跨域*/
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", corsConfiguration);
        System.out.println("*******配置跨域*******");
        return new CorsWebFilter(source);
    }

}
